package com.itstorm.pomoloweather.dao;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.itstorm.pomeloweather.entity.DataStatus;
import com.itstorm.pomeloweather.entity.WeatherData;
import com.itstorm.pomeloweather.entity.WeatherIndex;
import com.itstorm.pomeloweather.entity.WeatherResults;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

/**获取数据类*/
/**
 * @author Evans
 * 
 */
public class WeatherDao {

	/**
	 * 获得天气数据的状态
	 * 
	 * @param json
	 *            响应的http字符串
	 * @return 天气数据的状态
	 */
	public static DataStatus getDataStatus(String json) {
		// 天气的完全体对象
		DataStatus dataStatus = null;
		try {
			dataStatus = new DataStatus();
			// 创建json对象
			JSONObject jsonObject = new JSONObject(json);
			// 获取两个状态值；1，error；2，status；
			int error = jsonObject.getInt("error");
			String status = jsonObject.getString("status");
			// 判断两个状态值都复合解析标准
			if (error == 0 && status.equals("success")) {
				// 获得日期
				dataStatus.date = jsonObject.getString("date");
				// 构建List结果集合
				ArrayList resultes = new ArrayList<WeatherResults>();
				// 获得结果
				JSONArray resulte = new JSONObject(json)
						.getJSONArray("results");
				for (int i = 0; i < resulte.length(); i++) {
					// 天气实体类--》当前城市，pm2.5 ，ArrayList indexs ，ArrayList data
					WeatherResults weatherResults = new WeatherResults();
					// 根据结果的下标得到json对象
					JSONObject resultObject = (JSONObject) resulte.opt(i);
					// 拿到当前城市和pm2.5并装进天气实体类
					String currentCity = resultObject.getString("currentCity");
					String pm25 = resultObject.getString("pm25");
					weatherResults.setPm25(pm25);
					weatherResults.setCurrentCity(currentCity);
					// 构建ArrayList indexs对象来封装index中的内容
					ArrayList indexs = new ArrayList<WeatherIndex>();
					// 通过字段名得到json对象数组
					JSONArray index = resultObject.getJSONArray("index");
					if (index.length() != 0) {
						for (int j = 0; j < index.length(); j++) {
							// 天气指数，洗车，旅游，感冒，运动，紫外线
							WeatherIndex weatherIndex = new WeatherIndex();
							// 通过下标得到数组内的json对象
							JSONObject indexObject = (JSONObject) index.opt(j);
							// 得到对象中的相关属性
							String title = indexObject.getString("title");
							String zs = indexObject.getString("zs");
							String tipt = indexObject.getString("tipt");
							String des = indexObject.getString("des");
							weatherIndex.setTitle(title);
							weatherIndex.setZs(zs);
							weatherIndex.setTipt(tipt);
							weatherIndex.setDes(des);
							// 把得到的对象放入列表
							indexs.add(weatherIndex);
						}
						// 设置天气实体类中的列表参数为indexs
						weatherResults.setIndexs(indexs);
					}
					// 构建ArrayList datas对象
					ArrayList datas = new ArrayList<WeatherData>();
					// 通过字段名获取json对象数组
					JSONArray data = resultObject.getJSONArray("weather_data");
					if (data.length() != 0) {
						for (int k = 0; k < data.length(); k++) {
							// 获取天气状态与图片
							WeatherData weatherData = new WeatherData();
							// 通过下标得到数组内的json对象
							JSONObject dataObject = (JSONObject) data.opt(k);
							// 获取对象内的相关属性
							String date = dataObject.getString("date");
							String dayPictureUrl = dataObject
									.getString("dayPictureUrl");
							String nightPictureUrl = dataObject
									.getString("nightPictureUrl");
							String weather = dataObject.getString("weather");
							String wind = dataObject.getString("wind");
							String temperature = dataObject
									.getString("temperature");
							weatherData.setDate(date);
							weatherData.setdayPictureUrl(dayPictureUrl);
							weatherData.setnightPictureUrl(nightPictureUrl);
							weatherData.setWeather(weather);
							weatherData.setWind(wind);
							weatherData.setTemperature(temperature);
							// 把得到的对象放入列表
							datas.add(weatherData);
						}
						// 设置天气实体类中的列表参数为datas
						weatherResults.setData(datas);
					}
					// 把天气实体类对象添加到resultes列表
					resultes.add(weatherResults);
					// 设置天气的完全体对象里的列表参数为resultes
					dataStatus.setResultes(resultes);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return dataStatus;
	}

	/** 获取网络图片 */
	public static Bitmap getBitmap(String pictureUrl) {
		URL url = null;
		Bitmap bitmap = null;
		HttpURLConnection conn = null;

		try {
			url = new URL(pictureUrl);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			InputStream ips = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(ips);
			ips.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}
}
